import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

/**
 * @author WangHaohui
 * @time： 2018/8/9
 */
public class Produce {
    public static void main(String[] args) throws MQClientException, InterruptedException {
        //生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("rmq-group2");
        //name server ip+端口
        producer.setNamesrvAddr("10.80.92.117:9876");
        /*
          版本问题  不写会报错误   错误类型：MQClientException: Send [3] times, still failed
          解决：换成3.2.6即可，或者加入producer.setVipChannelEnabled(false)
          从3.4.*开始，默认开启了VIP通道，VIP通道端口为10911-2=10909。若Rocket服务器未启动端口10909，则报connect to <：10909> failed。
          这里要加producer.setVipChannelEnabled(false)
         */
        producer.setVipChannelEnabled(false);
        producer.start();

        for (int i = 0; i < 10000; i++) {
            try {
                //封装消息  topic: 主题  tags：标签  后面一个是发送内容
                Message msg = new Message("TopicTest",// topic
                        "TagA",// tag
                        ("Hello RocketMQ " + i).getBytes()// body
                );
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult);
            }
            catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }
        producer.shutdown();
    }
}
